home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / cclib / source / xwait.c < prev    next >
C/C++ Source or Header  |  1995-11-16  |  11KB  |  438 lines

  1.  
  2. /********** AMIGA STUFF ***************/
  3. #define ANSIC
  4. short _math = 0;
  5. #include "functions.h"
  6. /**************************************/
  7.  
  8.  
  9. #include "string.h"
  10. #include "time.h"
  11. #include "stdlib.h"
  12. #include "stdio.h"
  13. #include "ctype.h"
  14.  
  15. /*-------------------------------------------------------------------------+
  16.  |                                       |
  17.  | Name:    XWAIT                               |
  18.  | Purpose: waits until a specified time, or a key is pressed. If ^C  is   |
  19.  |        pressed a code of 20 is returned, if a key is pressed a code of|
  20.  |        10 is returned, if the time arrives to stop waiting then a 0   |
  21.  |        is returned. If an error occurs 30 is returned.           |
  22.  |                                       |
  23.  |        The command line arguments are given as a time and a date.       |
  24.  |        WAIT will stop waiting when the time is greater than or       |
  25.  |        equal to that given.                       |
  26.  |                                       |
  27.  |        Syntax: XWAIT <time> <date>                    |
  28.  |                                       |
  29.  |        Time is in HH:MM:SS                        |
  30.  |        Date is in MM:DD:YY or TOMORROW,SUN,MON,TUE,WED,THU,FRI,SAT    |
  31.  |                      TODAY is assumed               |
  32.  |                                       |
  33.  | Author:  RWA                    Date: 9/90           |
  34.  +-------------------------------------------------------------------------*/
  35.  
  36. #define CTRLC_EXIT 20
  37. #define CONTINUE_EXIT 10
  38. #define ERROR_EXIT 30
  39. #define TIMEOUT_EXIT 0
  40. #define SYNTAX "\nSyntax:\n\
  41.    XWAIT HH:MM[:SS] [[MM-DD-YY] TOMORROW,SUN,MON,TUE,WED,THU,FRI,SAT]"
  42. #define BANNER "\nXWAIT V1.0 By Robert W. Albrecht\n"
  43. #define SLEEP_SECONDS 1
  44. static char *help =
  45. " +----------------------------------------------------------------+\n"
  46. " | Waits until a specified time, or a key is pressed. If ^C is    |\n"
  47. " | pressed a code of 20 is returned, if a key is pressed a code of|\n"
  48. " | 10 is returned, if the time arrives to stop waiting then a 0   |\n"
  49. " | is returned. If an error occurs 30 is returned.                |\n"
  50. " |                                                                |\n"
  51. " | The command line arguments are given as a time and a date.     |\n"
  52. " | XWAIT will stop waiting when the time is greater than or       |\n"
  53. " | equal to that given.                                           |\n"
  54. " +----------------------------------------------------------------+\n";
  55.  
  56. typedef struct
  57. {
  58. int hour, minute, second;
  59. int month, day, year;
  60. } SetTime;
  61.  
  62. /************* SYSTEM DEPENDENT FUNCTIONS (NON-PORATBLE AMIGA) ************/
  63.  
  64. /*-------------------------------------------------------------------------+
  65.  |                                       |
  66.  | Name:    sleep_time                               |
  67.  | Purpose: puts the process to sleep for a prescribed period of time       |
  68.  |                                       |
  69.  | Author:  RWA                    Date: 9/90           |
  70.  +-------------------------------------------------------------------------*/
  71. static void sleep_time(int sec)
  72. {
  73. Delay(50L*((long)sec));
  74. }
  75.  
  76.  
  77. /*-------------------------------------------------------------------------+
  78.  |                                       |
  79.  | Name:    check_keyboard                           |
  80.  | Purpose: checks the keyboard for input, returns exit code if key pressed|
  81.  |                                       |
  82.  | Author:  RWA                    Date: 9/90           |
  83.  +-------------------------------------------------------------------------*/
  84. static int check_keyboard(void)
  85. {
  86. int rv = 0;
  87. #define SIGBREAK \
  88.    (SIGBREAKF_CTRL_C|SIGBREAKF_CTRL_D|SIGBREAKF_CTRL_E|SIGBREAKF_CTRL_F)
  89.  
  90. long signals;
  91.  
  92. if( (signals = SetSignal(0L,0L)) & SIGBREAK )
  93.    {
  94.    SetSignal(0L,(long)(signals & SIGBREAK));
  95.    if( signals & SIGBREAKF_CTRL_C )
  96.       rv = CTRLC_EXIT;
  97.    else
  98.       rv = CONTINUE_EXIT;
  99.    }
  100. return(rv);
  101. }
  102.  
  103. /********************* END OF SYSTEM DEPENDENT FUNCTIONS *****************/
  104.  
  105. /*-------------------------------------------------------------------------+
  106.  |                                       |
  107.  | Name:    month_days                               |
  108.  | Purpose: returns the number of days in a month based on the year       |
  109.  |                                       |
  110.  | Author:  RWA                    Date: 9/90           |
  111.  +-------------------------------------------------------------------------*/
  112. static int month_days(int month, int year)
  113. {
  114. int days;
  115. static short mo_days[12] =
  116. { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  117.  
  118. /* month starts at 1 */
  119. days = mo_days[month-1];
  120. if( month == 1 && !(year & 3) )
  121.       days++;
  122. return(days);
  123. }
  124.  
  125. /*-------------------------------------------------------------------------+
  126.  |                                       |
  127.  | Name:    get_stime                               |
  128.  | Purpose: translates a string into a numeric date and time           |
  129.  |        0 is returned if the input is valid                |
  130.  | Author:  RWA                    Date: 9/90           |
  131.  +-------------------------------------------------------------------------*/
  132. int get_stime(SetTime *settime, char *time_ptr)
  133. {
  134. int rv = 1;
  135. char *ptr;
  136.  
  137.  
  138. if( ptr = strchr(time_ptr,':') )
  139.    {
  140.    *ptr++ = '\0';
  141.  
  142.    settime->hour = atoi(time_ptr);  /* hours */
  143.  
  144.    if( settime->hour <= 23 && settime->hour >= 0 )
  145.       {
  146.       time_ptr = ptr;
  147.       if( ptr = strchr(time_ptr,':') )
  148.      *ptr++ = '\0';
  149.  
  150.       settime->minute = atoi(time_ptr);  /* minutes */
  151.  
  152.       if( settime->minute <= 59 && settime->minute >= 0 )
  153.      {
  154.      time_ptr = ptr;
  155.  
  156.      if( isdigit(*time_ptr) )
  157.         {
  158.         settime->second = atoi(time_ptr);  /* seconds (optional) */
  159.         if( settime->second < 0 || settime->second > 59 )
  160.            settime->second = 0;
  161.         }
  162.      else
  163.         settime->second = 0;
  164.  
  165.      rv = 0;
  166.      }
  167.       }
  168.    }
  169.  
  170. return(rv);
  171. }
  172.  
  173. /*-------------------------------------------------------------------------+
  174.  |                                       |
  175.  | Name:    get_keyword                            |
  176.  | Purpose: returnd a code >= 0 if the date is one of the date keywords    |
  177.  |                                       |
  178.  | Author:  RWA                    Date: 9/90           |
  179.  +-------------------------------------------------------------------------*/
  180.  
  181. static int get_keyword(char *date_ptr)
  182. {
  183. int i, rv;
  184. static char *keywords[] =
  185.    {"TOMORROW","SUN","MON","TUE","WED","THU","FRI","SAT",};
  186. strupr(date_ptr);
  187. for( i = 0, rv = -1; i < sizeof(keywords)/sizeof(char *); i++)
  188.    {
  189.    if( !strcmp(keywords[i],date_ptr) )
  190.       {
  191.       rv = i;
  192.       break;
  193.       }
  194.    }
  195. return(rv);
  196. }
  197.  
  198. /*-------------------------------------------------------------------------+
  199.  |                                       |
  200.  | Name:    add_days                               |
  201.  | Purpose: adds n days to a settime structure, n less than days in month  |
  202.  |                                       |
  203.  | Author:  RWA                    Date: 9/90           |
  204.  +-------------------------------------------------------------------------*/
  205.  
  206. static void add_days(SetTime *settime, struct tm *now, int n)
  207. {
  208. int mdays;
  209.  
  210. /* set up todays date */
  211. settime->month = now->tm_mon + 1;
  212. settime->day   = now->tm_mday;
  213. settime->year  = now->tm_year;
  214.  
  215. mdays = month_days(settime->month, settime->year);
  216.  
  217. if( (settime->day + n) > mdays )
  218.    {
  219.    n -= (mdays - settime->day);
  220.  
  221.    settime->day = n;
  222.  
  223.    if( (settime->month + 1) <= 12 )
  224.       settime->month++;
  225.    else
  226.       {
  227.       settime->year++;
  228.       settime->month = 1;
  229.       }
  230.    }
  231. else
  232.    settime->day += n;
  233. }
  234.  
  235. /*-------------------------------------------------------------------------+
  236.  |                                       |
  237.  | Name:    get_settime                            |
  238.  | Purpose: translates two strings into numeric date and time           |
  239.  |        0 is returned if the input is valid                |
  240.  | Author:  RWA                    Date: 9/90           |
  241.  +-------------------------------------------------------------------------*/
  242. static int get_settime(SetTime *settime, char *time_ptr, char *date_ptr)
  243. {
  244. int rv = 1, days, code;
  245. time_t tim;
  246. struct tm *tms_ptr;
  247. char *ptr;
  248.  
  249. if( !get_stime(settime,time_ptr) )
  250.    {
  251.    if( (tim = time(NULL)) != -1 )
  252.       {
  253.       tms_ptr = localtime(&tim);
  254.  
  255.       if( date_ptr )
  256.      {
  257.      code = get_keyword(date_ptr);
  258.      switch( code )
  259.         {
  260.         case 0:
  261.            add_days(settime,tms_ptr,1);
  262.            rv = 0;
  263.         break;
  264.  
  265.         case 1: case 2: case 3: case 4: case 5: case 6: case 7:
  266.            if( (tms_ptr->tm_wday + 1) < code )
  267.           days =  code - (tms_ptr->tm_wday + 1);
  268.            else
  269.           days = 7 - (tms_ptr->tm_wday + 1) + code;
  270.            add_days(settime,tms_ptr,days);
  271.            rv = 0;
  272.         break;
  273.  
  274.         case -1:
  275.            if( ptr = strchr(date_ptr,'-') )
  276.           {
  277.           *ptr++ = '\0';
  278.  
  279.           settime->month = atoi(date_ptr);
  280.  
  281.           if( settime->month >= 1 && settime->month <= 12 )
  282.              {
  283.              date_ptr = ptr;
  284.              if( ptr = strchr(date_ptr,'-') )
  285.             {
  286.             *ptr++ = '\0';
  287.  
  288.             settime->day = atoi(date_ptr);
  289.  
  290.             date_ptr = ptr;
  291.  
  292.             settime->year = atoi(date_ptr);
  293.             settime->year %= 100;
  294.  
  295.             if( settime->day >= 1 && settime->day <=
  296.                 month_days(settime->month,settime->year) )
  297.                rv = 0;
  298.             }
  299.              }
  300.           }
  301.         break;
  302.         }
  303.      }
  304.       else
  305.      { /* today */
  306.      settime->month = tms_ptr->tm_mon + 1;
  307.      settime->day    = tms_ptr->tm_mday;
  308.      settime->year    = tms_ptr->tm_year;
  309.      rv = 0;
  310.      }
  311.       }
  312.    }
  313. return(rv);
  314. }
  315.  
  316. /*-------------------------------------------------------------------------+
  317.  |                                       |
  318.  | Name:    compare_time                           |
  319.  | Purpose: compares the time in the SetTime to struct tm from localtime   |
  320.  |                                       |
  321.  | Author:  RWA                    Date: 9/90           |
  322.  +-------------------------------------------------------------------------*/
  323. static int compare_time(time_t *tim, SetTime *settime)
  324. {
  325. int rv;
  326. struct tm *tm_ptr;
  327.  
  328. tm_ptr = localtime(tim);
  329.  
  330. if( !(rv = settime->year - tm_ptr->tm_year) )
  331.    if( !(rv = settime->month - (tm_ptr->tm_mon + 1)) )
  332.       if( !(rv = settime->day - tm_ptr->tm_mday) )
  333.      if( !(rv = settime->hour - tm_ptr->tm_hour) )
  334.         if( !(rv = settime->minute - tm_ptr->tm_min) )
  335.            rv = settime->second - tm_ptr->tm_sec;
  336. return(rv);
  337. }
  338.  
  339. /*-------------------------------------------------------------------------+
  340.  |                                       |
  341.  | Name:    wait_time                               |
  342.  | Purpose: waits for a time to pass                       |
  343.  |                                       |
  344.  | Author:  RWA                    Date: 9/90           |
  345.  +-------------------------------------------------------------------------*/
  346. static int wait_time(SetTime *settime)
  347. {
  348. time_t tim;
  349. int waiting;
  350. int rv;
  351.  
  352. for(waiting = TRUE; waiting ; )
  353.    {
  354.    if( (tim = time(NULL)) != -1 )
  355.       {
  356.       if( compare_time(&tim,settime) <= 0 )
  357.      {
  358.      rv = TIMEOUT_EXIT;
  359.      waiting = FALSE;
  360.      }
  361.       else
  362.      if( !(rv = check_keyboard()) )
  363.         sleep_time(SLEEP_SECONDS);
  364.      else
  365.         waiting = FALSE;
  366.       }
  367.    else
  368.       {
  369.       rv = ERROR_EXIT;
  370.       waiting = FALSE;
  371.       }
  372.    }
  373. printf("Done\n");
  374. return(rv);
  375. }
  376.  
  377. /*-------------------------------------------------------------------------+
  378.  |                                       |
  379.  | Name:    main                               |
  380.  | Purpose: program entry point                        |
  381.  |                                       |
  382.  | Author:  RWA                    Date: 9/90           |
  383.  +-------------------------------------------------------------------------*/
  384. void main(int argc, char *argv[])
  385. {
  386. int exit_code;
  387. char *date_ptr, *time_ptr;
  388. char *exit_msg = NULL;
  389. SetTime settime;
  390.  
  391. if( argc > 0 )
  392.    printf(BANNER);
  393.  
  394. if( argc >= 2 )
  395.    {
  396.    time_ptr = argv[1];
  397.    if( argc == 3 )
  398.       date_ptr = argv[2];
  399.    else if( argc > 3 )
  400.       {
  401.       exit_msg = SYNTAX;
  402.       exit_code = ERROR_EXIT;
  403.       }
  404.    else
  405.       date_ptr = NULL;
  406.  
  407.    if( !get_settime(&settime,time_ptr,date_ptr) )
  408.       {
  409.  
  410.       printf("Waiting for %02d-%02d-%02d, %02d:%02d:%02d\n",
  411.      settime.month,settime.day,settime.year,
  412.      settime.hour,settime.minute,settime.second);
  413.       printf("   Press ^C to abort, another key to continue...");
  414.       fflush(stdout);
  415.       exit_code = wait_time(&settime);
  416.       }
  417.    else
  418.       {
  419.       exit_msg = SYNTAX;
  420.       exit_code = ERROR_EXIT;
  421.       }
  422.  
  423.    }
  424. else if( argc )
  425.    {
  426.    exit_msg = SYNTAX;
  427.    exit_code = ERROR_EXIT;
  428.    if( argc == 1 )
  429.       printf(help);
  430.    }
  431.  
  432. if( exit_msg )
  433.    printf("%s\n",exit_msg);
  434. exit(exit_code);
  435. }
  436.  
  437.  
  438.